<?php

    class Login extends MY_Controller {

        public function index()
        {


            $this->load->helper('common_function');


            $dopost=$this->input->post('dopost');

            if($dopost=="login")
            {
				$s_vcode= $this->session->userdata('vcode');
				if(strtolower($s_vcode)!= strtolower($this->input->post('vdcode', true)))
				{
					ShowMsg('验证码错误!', site_url("admin/login"), 0, 1000);
					exit();
				}
			
                $username = $this->input->post('username',true);
                $password = $this->input->post('password',true);
                //echo $this->session->userdata('vcode');
                if($username && $password)
                {
                    $this->load->model('admin/User_model');
                    $admin=$this->User_model->get_full_user_by_username($username);
                    if($admin)
                    {
                        if($admin->pwd==md5($password))
                        {
						
						//更新登录用户IP和登录时间
							$admin_data = array( 
								'logintime' => date("Y-m-d H:i:s"), 
								'loginip' => $this->input->ip_address()
							);
						 
							$this->User_model->admin_modify($admin->id,$admin_data);
						
							//将登录信息纪录到登录日志中
							$data_log = array( 
								'account' => $username, 
								'msg' => $this->input->user_agent(), 
								'access_ip' => $this->input->ip_address(), 
								'create_time' => date("Y-m-d H:i:s") 
							);
							$this->load->model('admin/System_model');
							$this->System_model->login_log_save_add($data_log);
						
                            $this->session->set_userdata('uid',$admin->id);
                            redirect('/admin/indexs', 'location', 301);
                        }
                        else
                        {
                            ShowMsg('密码错误!',-1,0,1000);
                            exit; 
                        }

                    }
                    else
                    {
                        ShowMsg('不存在的用户!',-1,0,1000);
                        exit; 
                    }


                }
                else
                {
                    ShowMsg('用户名和密码不能为空!',-1,0,1000);
                    exit;
                }



            }
            else

            {

                $this->load->view('admin/login');
            }


        }

        public   function quit()
        {
            $this->session->sess_destroy();
            redirect('admin/login');
        }

        public function vcode()
        {
            $this->load->library('Vcode');
            $this->session->set_userdata('vcode',  $this->vcode->codeword());
            $this->vcode->getimg();


            $this->output->enable_profiler(TRUE);


        }

    }
